
package zhuhai
{
	import flash.geom.Rectangle;
	import flash.display.NativeWindow;
	import flash.system.Capabilities;
	import flash.filesystem.File;
	import flash.events.KeyboardEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.display.StageQuality;
	import flash.display.StageAlign;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	[SWF(width = "720", height = "1280", frameRate = "24", backgroundColor = 0x333333)]
	public class Main extends Sprite
	{
		
		public static var main:Main;
		public static var header:Header;//标题
		private static var allfiles:Array;//assets下第一级目录文件路径数组
		
		public function Main()
		{
			main = this;
			if (stage) init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public static function get stageWidth():int
		{
			return Capabilities.screenResolutionX;
		}
		
		public static function get stageHeight():int
		{
			return Capabilities.screenResolutionY;
		}
		private function removeFromArr(arr:Array,s:String):String
		{
			if(arr && arr.length>0)
			{
				var i:int = 0;
				while(i<arr.length)
				{
					var _s:String = arr[i] as String;
					if(_s && _s.match(new RegExp("[\\\/]"+s+"\.(jpg$|png$|jpeg$)","i")))
					{
						arr.splice(i,1);
						return _s;
					}
					++i;
				}
			}
			return null;
		}
		
		private function init(e:Event = null):void
		{
			//全屏显示
			stage.nativeWindow.activate();
			stage.nativeWindow.bounds = new Rectangle(0, 0, stageWidth, stageHeight);
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
			stage.quality = StageQuality.HIGH;

			//背景
			graphics.beginFill(0x333333);
			graphics.drawRect(0, 0, stageWidth, stageHeight);
			graphics.endFill();

			addChild(new MyKey);//按键事件管理

			header = new Header("首页");//标题
			addChild(header);

			allfiles = SwfLoader.filesInDir("assets", ".*", false);//第一级目录

			var tx:int = 0;
			var ty:int = header.height;
			var tw:int = stageWidth / 3;
			var th:int = (stageHeight - ty) / 2;
			var i:int = 0;
			for each (var file:File in allfiles)
			{
				//按钮文字
				logs.adds(file.name);
				var txt:TextField = new TextField();
				txt.defaultTextFormat = new TextFormat(null, 30, 0xffffff);
				txt.width = tw;
				txt.height = th;
				txt.x = tx + (i % 3) * tw;
				txt.y = ty + int(i / 3) * th;
				txt.border = true;
				txt.text = file.name;
				txt.wordWrap = true;
				addChild(txt);
				

				//获取文件夹下图片，视频图信息
				var picturesArr:Array = SwfLoader.filesInDir(file.nativePath, SwfLoader.imgReg);
				var videosArr:Array = SwfLoader.filesInDir(file.nativePath, SwfLoader.vidReg, false);

				//添加背景图到舞台
				var bgURL:String = null;
				var iconURL:String = null;
				if (picturesArr && picturesArr.length > 0)
				{//取一张图作为背景图
					//bgURL = picturesArr.shift();
					//bgURL = picturesArr[0];
					bgURL = removeFromArr(picturesArr,"bg");
					iconURL = removeFromArr(picturesArr,"icon");
				}
				if (iconURL)
				{
					var xx:int = 0;
					var yy:int = 36;
					var bg:PhotoLoader = new PhotoLoader(iconURL, tw - xx * 2, th - yy * 2, true);
					addChildAt(bg, 0);
					bg.x = txt.x + xx;
					bg.y = txt.y + yy;
				}

				//保存文件夹下图片，视频，背景图信息
				dirs.push({videos: videosArr, pictures: picturesArr,iconURL:iconURL, bgURL: bgURL});
				
				++i;
			}
			addChild(new Border());//选择框

			MyKey.main.addEventListener(MyKey.CLICK, showBoard1);
		}
		
		private var dirs:Array = new Array();//保存二级文件夹的内容
		
		private function showBoard1(e:Event):void
		{
			if (main.visible)
			{
				main.visible = false;
				var file:File = allfiles[Border.index] as File;
				var videos:Array = dirs[Border.index].videos;
				var pictures:Array = dirs[Border.index].pictures;
				var bgURL:String = dirs[Border.index].bgURL;

				if (videos && videos.length > 0)
				{//文件夹存在视频，播放视频
					VideoPlay.show(videos[0]);
				}
				else
				{//没有视频，播放3D图片浏览
					Pictures.show(file, bgURL, pictures);
					
				}
				
			}
		}
		
		public static function show():void
		{//提供接口，显示主页
			main.stage.addChild(main);
			main.visible = true;
			logs.adds("Main show");
		}
	
	}
}
